﻿using System;
using System.Dynamic;
using System.Threading.Tasks;
using System.Web;
using System.Web.Configuration;
using Abp.Authorization;
using Abp.AutoMapper;
using Abp.Domain.Repositories;
using Abp.Runtime.Session;
using Abp.UI;
using SingleUserMall.Common;
using SingleUserMall.Stores;
using SingleUserMall.Stores.VO;

namespace SingleUserMall.Configuration
{
    [AbpAuthorize]
    public class ConfigurationAppService : SingleUserMallAppServiceBase, IConfigurationAppService
    {
        private readonly IRepository<Store> _storeRepository;
        public ConfigurationAppService(
            IRepository<Store> storeRepository
            )
        {
            _storeRepository = storeRepository;
        }
        public async Task<StoreVO> GetStoreInfoAsync(HasStoreForm form)
        {
            var store = await _storeRepository.FirstOrDefaultAsync(form.StoreId);
            return store.MapTo<StoreVO>();
        }

        [AbpAllowAnonymous]
        public async Task<string> UploadAsync([SwaggerFileUpload(true)] string file)
        {
            try
            {
                var _file = HttpContext.Current.Request.Files[0];
                var (fileUrl, path, size, key) = Upload(_file);
                QiniuContext.Instance.UploadFile(path, key);
                return await Task.FromResult(QiniuContext.Instance.GetUrl(key));

            }
            catch (Exception ex)
            {
                throw new UserFriendlyException(ex.Message);
            }
        }


        [AbpAllowAnonymous]
        public async Task<dynamic> GetVersion()
        {
            try
            {
                string apkNewVersion = WebConfigurationManager.AppSettings["apkNewVersion"];
                string ApkUrl = WebConfigurationManager.AppSettings["apkUrl"];
                string IOSUrl = "https://itunes.apple.com/us/app/518%E4%BA%A7%E5%9C%B0%E7%9B%B4%E5%8D%96/id1425790231?l=zh&ls=1&mt=8";
                string apkNotice = "APP测试版上线";
                dynamic res = new { apkNewVersion, ApkUrl, IOSUrl , apkNotice };
                return await Task.FromResult(res);
            }
            catch(Exception ex)
            {
                throw new UserFriendlyException(ex.Message);
            }
        }
    }
}
